package com.superhero.miniprogram.controller;

import cn.hutool.poi.excel.ExcelWriter;
import com.superhero.common.pojo.ApiResult;
import com.superhero.common.pojo.PageInput;
import com.superhero.common.pojo.entity.AccessRegistration;
import com.superhero.common.pojo.vo.HealthInfoVo;
import com.superhero.common.pojo.vo.InspectorEnd.AccessInfoInput;
import com.superhero.common.pojo.vo.InspectorEnd.InspectorInfo;
import com.superhero.common.pojo.vo.PageVo;
import com.superhero.common.pojo.vo.website.AccessStatisticVo;
import com.superhero.common.pojo.vo.website.TemperatureStatisticVo;
import com.superhero.common.utils.ExcelUtils;
import com.superhero.miniprogram.security.UserUtil;
import com.superhero.miniprogram.service.impl.InspectorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;


@Api(tags = {"检查人员端"})
@RestController
@RequestMapping("/miniprogram/inspector")
public class InspectorController {

    @Autowired
    InspectorServiceImpl inspectorService;

    @ApiOperation(value = "扫码获取用户基本信息")
    @GetMapping("/userHealthInfo")
    public ApiResult<HealthInfoVo> getHealthQRCode(@NotNull Integer userId) {
        HealthInfoVo vo = inspectorService.getHealthQRCode(userId);

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "登记出入人员信息")
    @PostMapping("/createAccessInfo")
    public ApiResult createAccessInfo(@NotNull Integer userId, @Validated AccessInfoInput accessInfoInput) {
        AccessRegistration accessInfo = inspectorService.createAccessInfo(userId, accessInfoInput);

        return ApiResult.success();
    }

    @ApiOperation(value = "获取进入人员出入信息统计")
    @GetMapping("/accessStatistic")
    public ApiResult<AccessStatisticVo> getAccessStatistic() {
        AccessStatisticVo vo = inspectorService.getAccessStatistic();

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "获取进入人员体温信息统计")
    @GetMapping("/temperatureStatistic")
    public ApiResult<TemperatureStatisticVo> getTemperatureStatistic() {
        TemperatureStatisticVo vo = inspectorService.getTemperatureStatistic();

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "获取进入人员列表信息", notes = "key可为人员姓名、检查人员姓名、出入类型（出/入），检查人员手机号，社区名称的全模糊查询")
    @GetMapping("/userHealthInfoList")
    public ApiResult<PageVo> getUserHealthInfoList(@Valid PageInput pageInput, @ApiParam(name = "人员类型 0-访客 1-住户") @NotNull Integer household, String keyword) {
        return ApiResult.success(inspectorService.getUserHealthInfoList(pageInput, household, keyword));
    }

    @ApiOperation(value = "导出进入人员列表信息", notes = "key可为人员姓名、检查人员姓名、出入类型（出/入），检查人员手机号，社区名称的全模糊查询")
    @GetMapping("/exportUserHealthInfoList")
    public void exportUserHealthInfoList(@ApiParam(name = "人员类型 0-访客 1-住户") @NotNull Integer household, String keyword, HttpServletResponse response) {
        ExcelWriter excelWriter = inspectorService.exportUserHealthInfoList(keyword, household, response);
        if (null != excelWriter) {
            // 进行 excel 写出操作。
            try {
                ExcelUtils.responseExcel("住户出入记录", response, excelWriter);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "获取检查人员基本信息")
    @GetMapping("/inspectorInfo")
    public ApiResult<InspectorInfo> getInspectorInfo() {
        InspectorInfo vo = inspectorService.getInspectorInfo();

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "修改密码")
    @PutMapping("/password")
    public ApiResult changePassword(@NotNull String oldPassword, @NotNull String newPassword) {
        String username = UserUtil.getUserUsername();
        inspectorService.changePassword(username, oldPassword, newPassword);

        return ApiResult.success();
    }

}
